﻿using System;
using System.Globalization;
using System.Web.UI.WebControls;
using ELearningSystem.UI.ContentServiceReference;
using ContentManager = ELearningSystem.UI.ContentServiceReference.ContentServiceSoapClient;
using AnswerManager = ELearningSystem.UI.AnswerServiceReference.AnswerServiceSoapClient;
using TreeContent = ELearningSystem.UI.ContentServiceReference.TreeContentDAO;

namespace ELearningSystem.UI.AdminUI
{
    public partial class AdminHomePage : System.Web.UI.Page
    {
        ContentManager contentManager = new ContentManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                messageLabel.Text = Request.QueryString["message"];
                positionTextBox.Text = contentManager.GetLastPosition(contentManager.GetCategoryNode()).ToString(CultureInfo.InvariantCulture);
                LoadTree();
                
            }
            

        }

        protected void saveTopicLinkButton_Click(object sender, EventArgs e)
        {
            TreeContent treeContent = new TreeContent();
            treeContent.Text = titleTextBox.Text;
            treeContent.Position = int.Parse(positionTextBox.Text);
            treeContent.ParentNode = contentManager.GetCategoryNode();
            if (contentManager.SaveOrUpdate(treeContent))
            {
                Response.Redirect("~/AdminUI/AdminHomePageUI.aspx?message=Success");
            }
            else
            {
                Response.Redirect("~/AdminUI/AdminHomePageUI.aspx?essage=Failed!!");
            }
        }

        //protected void addContentButton_Click(object sender, EventArgs e)
        //{
        //    Response.Redirect("~/AdminUI/AddContentUI.aspx");
        //}
        public void LoadTree()
        {
            treeDataSource.Data = contentManager.GetTreeDataSource();
            //treeDataSource.DataBind();
            //playlistTreeView.DataSource = treeDataSource;
            playlistTreeView.DataBindings.Add(new TreeNodeBinding() { DataMember = "CATEGORY", TextField = "name", ValueField = "ID" });
            playlistTreeView.DataBindings.Add(new TreeNodeBinding() { DataMember = "CONTENT", TextField = "name", ValueField = "ID" });
            playlistTreeView.DataBindings.Add(new TreeNodeBinding() { DataMember = "TOPIC", TextField = "name", ValueField = "ID" });
            playlistTreeView.DataBind();
            playlistTreeView.AutoGenerateDataBindings = true;
        }
    }
}